package Socket;

import CliGui.ChatGui;
import CliGui.ChoiceGui;
import CliGui.WaitingGui;

import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Cli {
    private static int flag = 0;

    private Socket cli;

    //第一次接收之前为-1，第一次接收的flag为当前local
    private int local = -1;

    private int choice;

    ChatGui chat;
    //


    public Cli(String ip,int port) throws IOException {
        cli = new Socket(ip,port);
    }

    public void startCLi() throws IOException, InterruptedException {
        SockUtil.sendMsg(cli,"success!");
        System.out.println("测试消息发送成功！");
        System.out.println("正在等待服务器连接数>=2...");

        //等待窗口,如果直接创建对象会在这里阻塞;
        waitGuiTask waitTask= new waitGuiTask();
        Thread waitT = new Thread(waitTask);
        waitT.start();


        //接收local
        local = Integer.parseInt(SockUtil.receiveMsg(cli));
        System.out.println("local"+local);

        while(true){
            System.out.println("ces");
            flag = Integer.parseInt(SockUtil.receiveMsg(cli));
            System.out.println("flag为"+flag);

            //当服务器flag=2时，才有两个客户端连接（0，1）
            if(flag >= 2){
                SockUtil.sendMsg(cli,"1");
                break;
            }
            else{
                SockUtil.sendMsg(cli,"0");
                continue;
            }
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("输入choice:");
        /*
        现在显示让用户选择一个聊天对象,然后接收和发送choice;
         */

        //关闭等待窗口，开启选择窗口
        WaitingGui.setFlagFalse();

        //选择窗口
       Thread Tchoice = new Thread(new choiceGuiTask());
       Tchoice.start();
        System.out.println(ChoiceGui.getChoice());
        //循环，判断用户是否输入choice
        while(true){
            System.out.println(ChoiceGui.getChoice());
            if(ChoiceGui.getChoice() != -5){
                choice = ChoiceGui.getChoice();
                System.out.println("choice 已赋值为"+choice);
                break;
            }
        }
        System.out.println("关闭choice窗口");




//                sc.nextInt();
//        sc.nextLine();//读换行



        //发choice
        SockUtil.sendMsg(cli,String.valueOf(choice));

        /*
        整一个msg变量，Chatgui中输入按确定后，更新msg并发送，以确定为信号
         */

        chat = new ChatGui();

        /*
        开启发送和接收线程
         */
        Thread send = new Thread(new cliSend());
        send.start();
        Thread recv = new Thread(new cliRecv());
        recv.start();



        //防止程序退出
        while(true);
    }

    //这个线程用来发送信息
    public class cliSend implements Runnable{

        @Override
        public void run() {
            Scanner sc = new Scanner(System.in);
            try {
                while(true){
                    System.out.println("chat的flag"+chat.getFlag());
                    if(chat.getFlag() == 1){
                        String msg = chat.getInputmsg();
                        SockUtil.sendMsg(cli,msg);
                        chat.setFlag(0);
                    }

//                    String msg = sc.nextLine();
//                    SockUtil.sendMsg(cli,msg);
                }

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //这个线程用来接收信息
    public class cliRecv implements Runnable{
        @Override
        public void run(){
            try {
                while(true){
                    String msg = SockUtil.receiveMsg(cli);
                    chat.getNewMsg(msg);
//                    System.out.println(msg);
                }

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //这个线程是显示等待窗口
    public class waitGuiTask implements Runnable{

        @Override
        public void run() {
            //等待窗口
            try {
                WaitingGui w = new WaitingGui();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public class choiceGuiTask implements Runnable{

        @Override
        public void run() {
            ChoiceGui c = new ChoiceGui(flag,local);
        }
    }


}
